package com.changgou.goods.service.impl;

import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.pojo.ParentCategory;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.pojo.Category;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 商品列表分页
     *
     * @param current  当前页
     * @param pageSize 每页展示条数
     * @return
     */
    @Override
    public Page<Category> findCategory(int current, int pageSize, int category_id) {
        PageHelper.startPage(current, pageSize);
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", category_id);
        return (Page<Category>) categoryMapper.selectByExample(example);
    }

    /**
     * 新增下级
     *
     * @param category
     */
    @Override
    public void insertCategory(Category category) {
        categoryMapper.insertSelective(category);
    }

    @Override
    public void delete(int id) {

    }

    @Override
    public List<ParentCategory> findAllCategory() {
        return null;
    }

    @Override
    public void addCatgory(Category category) {

    }

    @Override
    public List<ParentCategory> findsCategory() {
        List<Category> categories = categoryMapper.selectAll();
        ArrayList<ParentCategory> categoryList = new ArrayList<>();
        for (Category category : categories) {
            if (category.getParentId() == 0) {//如果是第一级分类
                ParentCategory parentCategory = new ParentCategory();
                parentCategory.setId(category.getId());
                parentCategory.setName(category.getName());
                ArrayList<ParentCategory> CategoryList1 = new ArrayList<>();
                for (Category category1 : categories) {
                    //查询出所有属于该第一级分类的下级分类
                    if (category.getId().equals(category1.getParentId())) {
                        ParentCategory parentCategory2 = new ParentCategory();
                        parentCategory2.setId(category1.getId());
                        parentCategory2.setName(category1.getName());
                        ArrayList<ParentCategory> CategoryList2= new ArrayList<>();
                        CategoryList1.add(parentCategory2);
                        for (Category category2 : categories) {
                            if (category.getId().equals(category2.getParentId())){
                                ParentCategory parentCategory3 = new ParentCategory();
                                parentCategory3.setId(category2.getId());
                                parentCategory3.setName(category2.getName());
                                CategoryList2.add(parentCategory3);
                            }
                        }
                        parentCategory2.setCategoryList(CategoryList2);
                        CategoryList1.add(parentCategory2);
                    }

                }
                parentCategory.setCategoryList(CategoryList1);
                categoryList.add(parentCategory);
            }

        }
        return categoryList;
    }

    @Override
    public void addCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public Category selectOneCategory(int id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return category;
    }

    /**
     * 前台首页导航栏查询
     * @return
     */
    @Override
    public List<Category> findNavigator() {
        List<Category> categoryList = categoryMapper.findNavigator();
        ArrayList<Category> categories  = new ArrayList<>();
        for (Category category : categoryList) {
            if (category.getParentId()==0){
//                categories.add(category);
                ArrayList<Category> childList = new ArrayList<>();
                for (Category category1 : categoryList) {
                    if(category1.getParentId().equals(category.getId())){
                        childList.add(category1);
                    }
                }
                category.setCategoryList(childList);
                categories.add(category);
            }
        }
        return categories;
    }

}
